package com.lagou.controller;

import com.lagou.domain.Menu;
import com.lagou.domain.ResponseResult;
import com.lagou.service.MenuService;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu() {

        List<Menu> allMenu = menuService.findAllMenu();

        return new ResponseResult(true, 200, "查询成功", allMenu);

    }

    @RequestMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(Integer id) {

        if (id == -1) {
            //添加操作 回显不需要查询 menu信息
            List<Menu> menuList = roleService.findSubMenuListByPid(-1);
            //封装数据
            Map<String, Object> map = new HashMap<>();
            map.put("menuInfo", null);
            map.put("parentMenuList", menuList);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
            return result;
        } else {
            //修改操作 回显
            Menu menu = menuService.findMenuById(id);
            List<Menu> menuList = roleService.findSubMenuListByPid(-1);
            Map<String, Object> map = new HashMap<>();
            map.put("menuInfo", menu);
            map.put("parentMenuList", menuList);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
            return result;
        }
    }

    @RequestMapping("/saveOrUpdateMenu")
    public ResponseResult saveOrUpdateMenu(@RequestBody Menu menu){

        if (menu.getId() == null){
            menuService.saveMenu(menu);
            return new ResponseResult(true,200,"新建菜单成功",null);
        }else {
            menuService.updateMenu(menu);
            return new ResponseResult(true,200,"修改菜单成功",null);
        }
    }
}
